<?php

namespace App\Core\Utils\Wechat;

use App\Core\Utils\TwHelper\CommFunction;
use Illuminate\Support\Facades\Redis;

class WechatConfig
{
    // 通威微信企业号相关配置
    const WECHAT_URL = 'https://qyapi.weixin.qq.com/cgi-bin/';
    const CORP_ID = 'wxe1f5d52564a082aa';
    const CORPSECRET = 'FKT8vBG3_7Zsep9lg7Oxp6NhZwe-GIzI8jpkVNAvbIvzIQsWJuiFPQLLmZhw2Nry';

    //redis缓存的key
    const WECHAT_ACCESS_TOKEN_KEY = 'tw:wechat:access:token:key';
    const WECHAT_ACCESS_TOKEN_TIME = 'tw:wechat:access:token:time';

    //微信后台应用的id
    const AGENTID_CLUB = '90'; // 通心粉社区应用id
    const AGENTID_YYCS = '40'; // 应用测试应用id
    const AGENTID_SCTX = '39'; // 市场体系应用id
    const AGENTID_JJYX = '45'; // 聚焦永祥应用id

    //长用部门id
    //新注册用户的部门
    const NEW_REGIST_DEPARTMENT = 7058;


    //操作接口的url配置
    //通过code获取userid
    const WECHAT_OPTION_GET_USER_ID = self::WECHAT_URL . 'user/getuserinfo';
    //通过userid获取用户信息
    const WECHAT_OPTION_USER_GET = self::WECHAT_URL . 'user/get';
    //添加用户
    const WECHAT_OPTION_USER_ADD = self::WECHAT_URL . 'user/create';
    //更新用户
    const WECHAT_OPTION_USER_UPDATE = self::WECHAT_URL . 'user/update';
    //删除用户
    const WECHAT_OPTION_USER_DEL = self::WECHAT_URL . 'user/delete';
    //批量删除用户
    const WECHAT_OPTION_USER_BATCH_DEL = self::WECHAT_URL . 'user/batchdelete';

    //添加部门
    const WECHAT_OPTION_DEPARTMENT_ADD = self::WECHAT_URL . 'department/create';
    //更新部门
    const WECHAT_OPTION_DEPARTMENT_UPDATE = self::WECHAT_URL . 'department/update';
    //更新部门
    const WECHAT_OPTION_DEPARTMENT_DEl = self::WECHAT_URL . 'department/delete';
    //查询部门
    const WECHAT_OPTION_DEPARTMENT_GET = self::WECHAT_URL . 'department/list';

    //获取微信AccessToken的方式 0:在laravel中生成；1:调用plus中封装的方法获取，
    //当plus停用后就可以采用laravel自己生成的方式
    const GET_WECHAT_ACCESS_TOKEN_WAY = 1;

    //用户注册写入微信企业号的orgid
    const REGIST_ORG_ID = 4384;
    //邀请用户注册写入微信企业号的orgid
    const WELCOME_REGIST_ORG_ID = 4385;
    //新注册写入微信企业号的orgid,该组织下面没有真实的用户姓名
    const NEW_REGIST_ORG_ID = 7058;


    /**
     * 获取企业号token
     * @return boolean
     */
    public static function getAccessToken()
    {
        if (self::GET_WECHAT_ACCESS_TOKEN_WAY == 1) {
            return self::getAccessTokenByTP();
        } else {
            return self::getAccessTokenByLaravel();
        }
    }

    /**
     * 获取企业号token
     * 调用plus中封装的方法获取
     * @return boolean
     */
    public static function getAccessTokenByTP()
    {
        $url = 'http://api.tongwei.cn/ApiWechatShare/getWechatAccessToken?token=jlf$ja%io2348903euiore2789423';
        $result = CommFunction::getCurl($url);
        if ($result['code'] == 0) {
            return $result['token'];
        }
        return false;
    }

    /**
     * 获取企业号token
     * 在laravel中生成
     * @return boolean
     */
    public static function getAccessTokenByLaravel()
    {
        if (Redis::exists(self::WECHAT_ACCESS_TOKEN_KEY) && Redis::exists(self::WECHAT_ACCESS_TOKEN_TIME)) {
            $wechatAccessToken = Redis::get(self::WECHAT_ACCESS_TOKEN_KEY);
            $wechatAccessTokenTime = Redis::get(self::WECHAT_ACCESS_TOKEN_TIME);//缓存的时间
            //微信的accessToken有效期是2个小时，2个小时后要重新生成
            if (!$wechatAccessToken || time() - $wechatAccessTokenTime > 7200) {
                return $wechatAccessToken = self::getAccessTokenAndSave();
            }
        } else {
            return $wechatAccessToken = self::getAccessTokenAndSave();
        }
        return false;
    }

    /**
     * 到腾讯企业号获取AccessToken 并保存到redis里面
     */
    public static function getAccessTokenAndSave()
    {
        $wechatAccessToken = self::makeAccessToken();
        if ($wechatAccessToken) {
            Redis::set(self::WECHAT_ACCESS_TOKEN_KEY, $wechatAccessToken);
            Redis::set(self::WECHAT_ACCESS_TOKEN_TIME, time());
        }
        return $wechatAccessToken;
    }

    /**
     * 到腾讯企业号获取AccessToken
     */
    public static function makeAccessToken()
    {
        $url = self::WECHAT_URL . 'gettoken?corpid=' . self::CORP_ID . '&corpsecret=' . self::CORPSECRET;
        $result = CommFunction::getCurl($url);
        $errcode = $result ['errcode'];
        if ($errcode) {
            return false;
        } else {
            return $result ['access_token'];
        }
    }


}